package com.myblog.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.myblog.Utils.AliOssUtil;
import com.myblog.Utils.PathUtils;
import com.myblog.Utils.SecurityUtils;
import com.myblog.domain.GlobalResponse;
import com.myblog.domain.entity.LoginUser;
import com.myblog.domain.entity.User;
import com.myblog.domain.entity.enums.GlobalResponseEnum;
import com.myblog.expection.GlobalException;
import com.myblog.services.UploadService;
import com.myblog.services.UserService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.URL;

@Service
public class UploadServiceImpl implements UploadService {

    @Resource
    UserService userService;
    @Override
    public GlobalResponse upload(MultipartFile img) {


        String originalFilename=img.getOriginalFilename();
        if(!originalFilename.endsWith("png")&&!originalFilename.endsWith("jpg")){
            throw new GlobalException(GlobalResponseEnum.FILE_TYPE_ERROR);
        }

        String path = PathUtils.setFilePath("img", originalFilename);
        URL url=null;
        try {
            url=AliOssUtil.UpLoadAvatar(img,path);
        } catch (IOException e) {
            throw new GlobalException(GlobalResponseEnum.ERROR.getCode(),"上传时错误");
        }
//        LambdaUpdateWrapper<User> wrapper=new LambdaUpdateWrapper<>();
//        wrapper.eq(User::getId,userid).set(User::getAvatar,url.toString());
//        userService.update(null,wrapper);
        return GlobalResponse.ok(url.toString());
    }


}
